+function ($) {
	"use strict";
	$.fn.transitionEnd = function(callback) {
		var events = ['webkitTransitionEnd', 'transitionend', 'oTransitionEnd', 'MSTransitionEnd', 'msTransitionEnd'],
			i, dom = this;
		function fireCallBack(e) {
			/*jshint validthis:true */
			if (e.target !== this) return;
			callback.call(this, e);
			for (i = 0; i < events.length; i++) {
				dom.off(events[i], fireCallBack);
			}
		}
		if (callback) {
			for (i = 0; i < events.length; i++) {
				dom.on(events[i], fireCallBack);
			}
		}
		return this;
	};

	$.support = (function() {
		var support = {
			touch: !!(('ontouchstart' in window) || window.DocumentTouch && document instanceof window.DocumentTouch)
		};
		return support;
	})();

	$.touchEvents = {
		start: $.support.touch ? 'touchstart' : 'mousedown',
		move: $.support.touch ? 'touchmove' : 'mousemove',
		end: $.support.touch ? 'touchend' : 'mouseup'
	};

	$.getTouchPosition = function(e) {
		e = e.originalEvent || e; //jquery wrap the originevent
		if(e.type === 'touchstart' || e.type === 'touchmove' || e.type === 'touchend') {
			return {
				x: e.targetTouches[0].pageX,
				y: e.targetTouches[0].pageY
			};
		} else {
			return {
				x: e.pageX,
				y: e.pageY
			};
		}
	};

	$.fn.scrollHeight = function() {
		return this[0].scrollHeight;
	};

	//Infinite
	var getOffset = function (container) {
		var tagName = container[0].tagName.toUpperCase();
		var scrollTop;
		if (tagName === 'BODY' || tagName === 'HTML') {
			scrollTop = container.scrollTop() || $(window).scrollTop();
		} else {
			scrollTop = container.scrollTop();
		}
		var offset = container.scrollHeight() - ($(window).height() + scrollTop);
		return offset;
	}

	var Infinite = function(el, distance) {
		this.container = $(el);
		this.container.data("infinite", this);
		this.distance = distance || 50;
		this.attachEvents();
	}

	Infinite.prototype.scroll = function() {
		var container = this.container;
		this._check();
	}
  
	Infinite.prototype.attachEvents = function(off) {
		var el = this.container;
		var scrollContainer = (el[0].tagName.toUpperCase() === "BODY" ? $(document) : el);
		scrollContainer[off ? "off" : "on"]("scroll", $.proxy(this.scroll, this));
	};
	Infinite.prototype.detachEvents = function(off) {
		this.attachEvents(true);
	}
	Infinite.prototype._check = function() {
		var offset = getOffset(this.container);
		if(Math.abs(offset) <= this.distance) {
			this.container.trigger("infinite");
		}
	}
  
	var infinite = function(el) {
		attachEvents(el);
	}
  
	$.fn.infinite = function(distance) {
		return this.each(function() {
			new Infinite(this, distance);
		});
	}
	$.fn.destroyInfinite = function() {
		return this.each(function() {
			var infinite = $(this).data("infinite");
			if(infinite && infinite.detachEvents) infinite.detachEvents();
		});
	}
}($);

var myLayer = {
	alert: function(content,time,callback){
		if($(".c-alert-box").is(":visible")){
			$(".c-alert-box").remove();
		}
		if(time == undefined || time == "" || time == null){
			time = 2000;
		}
		var ahtml = '<div class="c-alert-box">'+content+'</div><div class="c-al-screen"></div>';
		$("body").append(ahtml);
		var aleL = ($(window).width() - $(".c-alert-box").width() - 20) / 2;
		$(".c-alert-box").css('left', aleL + "px");
		setTimeout(function(){
			$(".c-alert-box,.c-al-screen").remove();
			if (callback){callback();}
		},time);
	},
	load: function(content){
		if($(".c-load-box").is(":visible")){
			$(".c-load-box").remove();
		}
		if(content == undefined || content == "" || content == null){
			content = "\u52a0\u8f7d\u4e2d..."
		}
		var lhtml = '<div class="c-load-box"><span class="loadgif"></span><p>'+content+'</p></div><div class="c-al-screen"></div>';
		$("body").append(lhtml);
		var totW = $(window).width();
		var aleL = (totW - $(".c-load-box").width() - 60) / 2;
		$(".c-load-box").css('left', aleL + "px");
	},
	clear: function(){
		$(".c-load-box,.c-al-screen").remove();
	},
	confirm: function(options){
		var dft= {
			title:'',
			con:'',
			cancel: null,
			cancelValue:'\u53d6\u6d88',
			ok: null,
			okValue:'\u786e\u5b9a'
		}
		var ops = $.extend(dft,options);
		var chtml = '<div class="c-conf-screen"></div>';
		chtml += '<div class="c-conf-box">';
		if(ops.title != ""){
			chtml += '<div class="conftitle">'+ops.title+'</div>';
		}
		chtml += '<div class="confcontent">'+ops.con+'</div>';
		if(ops.cancel != null){
			chtml += '<div class="c-confbtn"><a href="javascript:;" id="popcanclebtn">'+ops.cancelValue+'</a><a href="javascript:;" id="popsurebtn">'+ops.okValue+'</a></div>';
		}else{
			chtml += '<div class="c-confbtn"><a href="javascript:;" id="popsurebtn">'+ops.okValue+'</a></div>';	
		}
		chtml += '</div></div>';
		$("body").append(chtml);
		$("#popcanclebtn").click(function(){
			if (ops.cancel() == false) {return false;}
			$(".c-conf-box,.c-conf-screen").remove();
		});
		$("#popsurebtn").click(function(){
			if (ops.ok() == false) {return false;}
			$(".c-conf-box,.c-conf-screen").remove();
		});
	}
}

function throttle(fn, delay){
	var timer = null;
	return function(){
		var context = this, args = arguments;
        clearTimeout(timer);
        timer = setTimeout(function(){
            fn.apply(context, args);
        }, delay);
    };
};

$(function(){
	$('.full-page').height($(window).height());
	$(window).resize(function(){
		$('.full-page').height($(window).height());
	});
});